package com.wzcl.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.wzcl.app.dao.model.AppUser;
import com.wzcl.app.dao.model.Channel;
import com.wzcl.app.dao.repository.ChannelRepository;
import com.wzcl.app.service.ChannelService;
import com.wzcl.app.service.common.PolyvService;
import com.wzcl.app.service.common.model.body.ChannelAuthSettingBody;
import com.wzcl.app.service.common.model.body.ChannelBasicSettingBody;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Log4j2
@Service
@Transactional
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    private ChannelRepository repository;

    @Autowired
    private PolyvService polyvService;

    @Override
    public synchronized Channel takeAvailable(ChannelBasicSettingBody basicBody, ChannelAuthSettingBody authBody) {
        List<Channel> available = repository.findAllByAvailable(true);
        Channel channel = null;
        if (available != null && available.size() > 0) {
            //有空闲直播频道
            channel = available.get(0);
            //设置频道类型
            if (!channel.getScene().equals(basicBody.getScene()))
                polyvService.updateDetail(channel.getChannelId(), "scene", basicBody.getScene());
            //恢复默认设置
            polyvService.updateChannel(channel.getChannelId(), basicBody, authBody);
            //修改表中信息
            channel.setAvailable(false);
            channel.setScene(basicBody.getScene());
            log.info("Channel:{} assign from existing resource pool", channel.getChannelId());
            return repository.save(channel);
        }
        //没有空闲则新建一个
        channel = repository.save(polyvService.createChannel(basicBody, authBody));
        polyvService.updateChannel(channel.getChannelId(), basicBody, authBody);
        log.info("Resource pool expansion! Add channel:{}", channel.getChannelId());
        return repository.save(channel);
    }

    @Override
    public synchronized void putAvailableByChannelId(Integer channelId) {
        Channel channel = repository.findByChannelId(channelId);
        //设置为无状态
        polyvService.end(channelId);
        channel.setAvailable(true);
        repository.save(channel);
        log.info("Channel:{} Return the resource pool", channel.getChannelId());
    }


    @Override
    public void updateAvailablePassword(Integer channelId, String password) {
        polyvService.setChannelName(channelId, password);
    }

    @Override
    public void setChannelMaxViewer(Integer channelId, Integer maxViewer) {
        polyvService.setChannelMaxViewer(channelId, maxViewer);
    }

    @Override
    public String getChannelInfo(Integer channelId) {
        return JSON.toJSONString(polyvService.getChannel(channelId));
    }

    @Override
    public Channel getChannelByChannelId(Integer channelId) {
        return repository.findByChannelId(channelId);
    }

    @Override
    public String getWatchUrl(Integer channelId, AppUser user) {
        return polyvService.getWatchUrl(channelId, user);
    }


}
